(function () {
    window.network = {
        socket: null,
        isConnect: false,
        msgQueue: [],
        updateTimer: null,
        isStop: false,
        failureCallback: null,
        disconnectCallback: null,
        register: {},
    };
    network.addListener = function (target, code, callback) {
        if (!network.register.hasOwnProperty(target)) {
            network.register[target] = {};
        }
        if (!network.register[target].hasOwnProperty(code)) {
            network.register[target][code] = [];
        }
        network.register[target][code].push(callback);
    };
    network.removeListener = function (target) {
        if (network.register.hasOwnProperty(target)) {
            delete network.register[target];
        }
    };
    network.start = function () {
        network.isStop = false;
        console.log("network.start()...");
    };
    network.stop = function () {
        network.isStop = true;
        console.log("network.stop()...");
    };
    network.send = function (code, data) {
        if (network.socket && network.isConnect) {
            var sendObj = {};
            sendObj.code = code;
            sendObj.data = data || {};
            if (DataManager.ownerUid) {
                sendObj.data.uid = DataManager.ownerUid;
            }
            var message = JSON.stringify(sendObj);
            network.socket.emit("message", message);
            console.log("[" + window.getCurTimestampString() + "] " + "send: " + message);
        }
    };
    network.recv = function (obj) {
        network.msgQueue.push(obj);
    };
    network.onMessageArrived = function (message) {
        console.log("[" + window.getCurTimestampString() + "] " + "recv: " + message);
        network.msgQueue.push(JSON.parse(message));
        // network.update();
    };
    network.update = function () {
        if (network.isConnect && !network.isStop) {
            while (network.msgQueue.length > 0) {
                if (network.isConnect && !network.isStop) {
                    var obj = network.msgQueue.shift();
                    var name = obj.code;
                    var data = obj.data;
                    for (var id in network.register) {
                        var obj = network.register[id];
                        if (obj.hasOwnProperty(name)) {
                            var handlerList = obj[name];
                            for (var j = 0; j < handlerList.length; j++) {
                                (function (j) {
                                    handlerList[j](data);
                                })(j);
                            }
                        }
                    }
                } else {
                    break;
                }
            }
        }
    };
    network.connect = function (url, successCallback, failureCallback) {
        if (network.socket) return;
        network.failureCallback = failureCallback;
        network.socket = io(url);
        network.socket.on("connect", function () {
            console.log("连接成功");
            network.isConnect = true;
            network.isStop = false;
            if (network.updateTimer) {
                clearInterval(network.updateTimer);
                network.updateTimer = null;
            }
            network.updateTimer = setInterval(network.update, 100);;
            if (successCallback) {
                successCallback();
            }
        });
        network.socket.on("connecting", function () {
            console.log("正在连接");
        });
        network.socket.on("disconnect", function () {
            network.isConnect = false;
            network.isStop = true;
            if (network.updateTimer) {
                clearInterval(network.updateTimer);
                network.updateTimer = null;
            }
            if (network.disconnectCallback) {
                network.disconnectCallback();
            }
            console.log("断开连接");
        });
        network.socket.on("connect_failed", function () {
            network.isConnect = false;
            network.isStop = true;
            if (failureCallback) {
                failureCallback();
            }
            console.log("连接失败");
        });
        network.socket.on("error", function (error) {
            console.log(error)
            console.log("错误发生，并且无法被其他事件类型所处理");
        });
        network.socket.on("reconnect", function () {
            network.isConnect = true;
            network.isStop = false;
            console.log("成功重连");
        });
        network.socket.on("reconnecting", function () {
            console.log("正在重连");
        });
        network.socket.on("reconnect_failed", function () {
            console.log("重连失败");
        });
        network.socket.on("message", function (data) {
            network.onMessageArrived(data);
        });
    };
})();